<?php

/**
 * Msa_LocationObject - Classe de gestion des lieux
 *
 * @package
 * @author Alexis Eric, 
 * Erra Prod. http://www.erraprod.com
 * @copyright Copyright (c) 2012 - Erra Prod
 * @access public
 */
class Msa_LocationObject extends Erra_Fields_Base_Object {

    public static $_locationList = array();

    function __construct($objectId, $buildArgs = null) {
        parent::__construct($objectId, $buildArgs);
        $nom = Erra_Fields_Text::create('nom')
                ->setDescription("Nom de l'endroit *")
                ->setInAdminList(true)
                ->setDatabaseType('VARCHAR', 128)
                ->setSize(40);
        
        $magasin_description = Erra_Fields_TextArea::create('magasin_description')
                ->setCols(70)
                ->setDatabaseType('TEXT')
                ->setDescription("Description de l'endroit");

        $image = Erra_Fields_ImageBank::create('image')
                ->setDescription('Image de la boutique')
                ->setWidth(150)
                ->setHeight(150)
                ->setCropped(true)
                ->setDatabaseType('VARCHAR', 256)
                ->setDir('image_banks/boutique');

        $adresse = Erra_Fields_Text::create('adresse')
                ->setDescription("Adresse *")
                ->setInAdminList(true)
                ->setDatabaseType('VARCHAR', 128)
                ->setSize(40);

        $ville_cp = Erra_Fields_Text::create('ville_cp')
                ->setDescription("Ville / Code Postal *")
                ->setInAdminList(true)
                ->setDatabaseType('VARCHAR', 32)
                ->setSize(40);

        $telephone = Erra_Fields_Text::create('telephone')
                ->setDescription("Téléphone *")
                ->setDatabaseType('VARCHAR', 32)
                ->setSize(40);

        $acces = Erra_Fields_TextArea::create('acces')
                ->setDescription("Informations d'accès")
                ->setCols(40)
                ->setRows(2)
                ->setDatabaseType('VARCHAR', 128); 

        $infos_complementaires = Erra_Fields_TextArea::create('infos_complementaires')
                ->setCols(60)
                ->setRows(2)
                ->setDescription('Informations complémentaires')
                ->setDatabaseType('TEXT');
        
        $redirection_url = Erra_Fields_Text::create('redirection_url')
                ->addAttribute('data-genre', 'general_element')
                ->setSize(60)
                ->setDescription('URL de redirection post-reservation <font color="red">(doit commencer par http:// ou https://)</font>')
                ->setDatabaseType('VARCHAR', 256);
        
        $promo_content = Erra_Fields_TextArea::create('promo_content')
                ->setCols(60)
                ->setDescription("Promotion (affichée à la fin d'une commande client)")
                ->setDatabaseType('TEXT');   
        
        $this->addFields($nom, $magasin_description, $image, $adresse, $ville_cp, $telephone, $acces, $infos_complementaires, $redirection_url, $promo_content);
        $this->setDescription('Vos lieux');
        $this->setAddText('Cliquez ici pour ajouter un lieu');
        $this->setCrud('CRUDS');
    }

    public function postInit() {
        $this->setDescription('Editer un lieu');
    }

    /**
     * getLocationArray() - Renvoie un tableau asscoatif id=>location_name pour chaque lieu
     * @return type
     */
    public static function getLocationArray($givenLocation = null) {
        $resultArray = array();
        if (empty(self::$_locationList)) {
            self::$_locationList['-'] = '-';
            $location_list = Erra_Core_DatabasePdo::SELECT('SELECT * FROM msa_locationobject');
            if (Erra_Core_DatabasePdo::rowCount()) {
                while ($row = $location_list->fetch()) {
                    if (!is_numeric($givenLocation) || (is_numeric($givenLocation) && $row->id == $givenLocation)) {
                        self::$_locationList[$row->id] = $row->nom;
                    }
                }
            }
            $resultArray = self::$_locationList;
        } else {
            $resultArray = self::$_locationList;
        }
        return $resultArray;
    }

    /**
     * Retourne un tableau complet avec les informations pour un lieu
     * 
     * @param type $givenLocation L'id du lieu surlequel on veut récuperer les informations
     * @return type
     */
    public static function getFullArray($givenLocation = null) {
        $resultArray = array();
        $query = (is_numeric($givenLocation)) ? 'SELECT * FROM msa_locationobject WHERE id=' . $givenLocation : 'SELECT * FROM msa_locationobject';
        $location_list = Erra_Core_DatabasePdo::SELECT($query);
        if (Erra_Core_DatabasePdo::rowCount()) {
            while ($row = $location_list->fetch()) {
                $row->image = config_get('public_upload_directory') . '/' . $row->image;
                $resultArray[] = $row;
            }
        }
        return $resultArray;
    }

}
